package com.laopan.wxpay.utils;

import java.security.SecureRandom;
import java.util.Random;

/**
 * 随机字符串生成工具类
 *
 * @author laopan
 * @className NonceCodeUtils
 * @date 2021/2/22 17:06
 */

public class NonceCodeUtils {
    private NonceCodeUtils() {
    }

    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final Random RANDOM = new SecureRandom();

    public static String generateNonceStr(int count) {
        if (count <= 0) {
            count = 32;
        }
        char[] nonceChars = new char[count];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }

    public static String generateNonceStr() {
        return generateNonceStr(32);
    }
}
